package com.wswy.wzcx.module.base

import android.app.Activity
import android.arch.lifecycle.LiveData
import com.che.libcommon.utils.RxBus
import com.che.libcommon.utils.ToastUtils
import com.wswy.wzcx.model.PaymentOrder
import com.wswy.wzcx.model.pay.PayStyleModel
import com.wswy.wzcx.module.InternalMsgCode
import com.wswy.wzcx.statistics.BizReport
import com.wswy.wzcx.ui.data.PayResult
import com.wswy.wzcx.ui.other.PayControl
import io.reactivex.observers.ResourceObserver
import timber.log.Timber

open class PayViewModel : UserViewModel() {

    class OrderPayResult(val status:Boolean,val order: PaymentOrder)

    private val orderPayLiveData = SingleLiveData<OrderPayResult?>()

    @Volatile
    private var isPaying = false
//    set(value) {
//
//        val ret = kotlin.runCatching {
//            throw RuntimeException("----set---${value}")
//        }
//        Timber.e(ret.exceptionOrNull())
//
//        field = value
//    }


    //支付订单
    var paymentOrder:PaymentOrder?=null
    private set

    override fun onCreate() {
        super.onCreate()

        val payResultObserver = object : ResourceObserver<PayResult>() {
            override fun onNext(payResult: PayResult) {

                onPayResult(payResult)
            }

            override fun onError(e: Throwable) {
                isPaying=false
            }

            override fun onComplete() {

            }
        }

        RxBus.getDefault().toObservableWithCode(InternalMsgCode.CODE_PAY_RESULT, PayResult::class.java).subscribe(payResultObserver)

        addRun(payResultObserver)
    }




    private fun onPayResult(payResult: PayResult){

        if (payResult.isPaySuccess){
            onPaySuccess()
        }else{
            if (!payResult.isCancel){
                when(payResult.payStyle){
                    PayStyleModel.WECHAT.code -> {
                        BizReport.sendException(BizReport.PAY_ERROR,"wechat:${paymentOrder}")
                    }
                    PayStyleModel.ALIPAY.code -> {
                        BizReport.sendException(BizReport.PAY_ERROR,"alipay:${paymentOrder}")
                    }
                }
            }
            ToastUtils.showText(payResult.msg)
            onPayFail()
        }
    }

     private fun onPaySuccess(){
        isPaying = false

         paymentOrder?.let {
            orderPayLiveData.postValue(OrderPayResult(true,it))
        }

         Timber.e("onPaySuccess---- $isPaying")
    }

    private fun onPayFail(){
        isPaying = false

        paymentOrder?.let {
            orderPayLiveData.postValue(OrderPayResult(false,it))
        }
        Timber.e("onPayFail---- $isPaying")
    }


    fun startPay(act:Activity,paymentOrder: PaymentOrder){
        Timber.e("----startPay---$isPaying")
        if (isPaying){
            return
        }
        Timber.e("----startPay---run---")
        this.paymentOrder = paymentOrder
        isPaying = true

        when(paymentOrder.payStyle){

            PayStyleModel.WECHAT.code -> {
                PayControl.requestPayWeChat(act, paymentOrder.pay)
            }
            PayStyleModel.ALIPAY.code -> {
                PayControl.requestPayAli(act, paymentOrder.pay)
            }
            //增加配置
            PayStyleModel.WECHAT_CHECAR.code,PayStyleModel.ALIPAY_CHECAR.code -> {
                PayControl.requestPay(act,paymentOrder)
            }
            else -> {
                isPaying = false
                ToastUtils.showText("请选择支付类型")
            }
        }
    }


    fun consumeOrderPayResult(){
        orderPayLiveData.postValue(null)
        isPaying = false
    }
    fun getOrderPayResultLiveData():LiveData<OrderPayResult?> = orderPayLiveData

}